<?php

namespace App\Repository\Order;

use App\Models\Order\OrderInfoModel as model;
use Illuminate\Support\Facades\Cache;
use App\Repository\RoleRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\BaseRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\UploadRepository;

class OrderInfoRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model();
    parent::__construct();
  }
  //添加 订单插入
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'orderno',
      'order_id',
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'order_id' => '订单id不能为空',
      'orderno' => '订单编号不能为空',
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    $isTrue = $this->model->where(['order_id' => $insertData['order_id']])->first();

    if ($isTrue) {
      $update['channel_id'] = $insertData['channel_id'];
      $isRes = $this->model->where(['order_id' => $insertData['order_id']])->update($update);
      if ($isRes) {
        return $this->returnMsg(200, '提交成功');
      }
      return $this->returnMsg(1, '提交失败');
    }
    if (!empty($data['pay_identity'])) {
      // 年龄 性别
      $insertData['sex'] = getIdentitySex($data['pay_identity']);
      $insertData['age'] = getIdentityAge($data['pay_identity']);
    }
    if (empty($insertData['age'])) {
      $insertData['age'] = 0;
    }
    $insertData['time'] = date('H');
    // $insertData['shop_str'] = $data['shop_str'];
    $res = $this->model->insert($insertData);
    if ($res) {
      return $this->returnMsg(200, '提交成功');
    }
    return $this->returnMsg(1, '提交失败');
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);

    //先查询是否存在
    if (!empty($field['status']) && $field['status'] == 1) {
      $field['three_time_at'] = date('Y-m-d H:i:s');
    }
    if (!empty($field['status']) && $field['status'] == 20) {
      $field['deliver_at'] = date('Y-m-d H:i:s');
    }
    if (!empty($field['status']) && $field['status'] == 30) {
      $field['sign_at'] = date('Y-m-d H:i:s');
    }
    if (!empty($field['status']) && $field['status'] == 40 && empty($field['activat_at'])) {
      $field['activat_at'] = date('Y-m-d H:i:s');
    }

    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->first();
  }
  public function getModel()
  {
    return $this->model;
  }
}
